﻿using Engine;
using Game;

namespace MFTrain
{
    public class SubsystemRodToTrainBehavior : SubsystemBlockBehavior
    {
        public override int[] HandledBlocks => new int[1] { 195 };

        public override bool OnUse(Ray3 ray, ComponentMiner componentMiner)
        {
            object obj = componentMiner.Raycast(ray, RaycastMode.Digging);
            if (obj is BodyRaycastResult)
            {
                ComponentMFTrain componentMFTrain;
                if ((componentMFTrain = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponentMFTrain>()) != null)
                {
                    componentMFTrain.ThrowAllItems();
                    base.Project.FindSubsystem<SubsystemPickables>().AddPickable(544, 1, ((BodyRaycastResult)obj).ComponentBody.Position + new Vector3(0f, 0.5f, 0f), null, null);
                    base.Project.RemoveEntity(((BodyRaycastResult)obj).ComponentBody.Entity, disposeEntity: true);
                    base.Project.FindSubsystem<SubsystemAudio>().PlaySound("Audio/BlockPlaced", 1f, 0f, componentMFTrain.m_componentBody.Position, 3f, autoDelay: true);
                    return true;
                }

                ComponentCar componentCar;
                if ((componentCar = ((BodyRaycastResult)obj).ComponentBody.Entity.FindComponent<ComponentCar>()) != null)
                {
                    base.Project.FindSubsystem<SubsystemPickables>().AddPickable(965, 1, ((BodyRaycastResult)obj).ComponentBody.Position + new Vector3(0f, 0.5f, 0f), null, null);
                    base.Project.RemoveEntity(((BodyRaycastResult)obj).ComponentBody.Entity, disposeEntity: true);
                    base.Project.FindSubsystem<SubsystemAudio>().PlaySound("Audio/BlockPlaced", 1f, 0f, componentCar.m_componentBody.Position, 3f, autoDelay: true);
                    return true;
                }
            }

            return base.OnUse(ray, componentMiner);
        }
    }
}